﻿using Candy.Infrastructure;
using Candy.Model;
using Candy.Web.Data;
using GalaSoft.MvvmLight.Threading;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.ServiceModel.DomainServices.Client;

namespace Candy.ViewModel
{
    public class FamilyEditViewModel : EntityEditViewModel<FamilyDto>
    {


        private ObservableCollection<FamilyEditViewModel> _subFamilies = new ObservableCollection<FamilyEditViewModel>();

        public ObservableCollection<FamilyEditViewModel> SubFamilies
        {
            get
            {
                return _subFamilies;
            }
            set
            {
                _subFamilies = value;
                RaisePropertyChanged("SubFamilies");
            }
        }


     
        INavigationService _navigationService;
        public FamilyEditViewModel(IDataService dataService, INavigationService navigationService, EntitySet<FamilyDto> familySet, FamilyDto family)
            : base(dataService, family, familySet)
        {
            _navigationService = navigationService;
            family.SubFamilies.EntityAdded += SubFamilies_EntityAdded;
            family.SubFamilies.EntityRemoved += SubFamilies_EntityRemoved;
            base.Canceled += FamilyEditViewModel_Canceled;
            base.Saved += FamilyEditViewModel_Saved;
            foreach (var item in family.SubFamilies.OrderBy(x=>x.Name))
            {
                FamilyEditViewModel vm = new FamilyEditViewModel(_dataService, _navigationService, _dataService.Families, item);
                _subFamilies.Add(vm);
            }
        }

        void FamilyEditViewModel_Saved(object sender, EventArgs e)
        {
            _navigationService.CloseFamilyEditor();
        }

        void FamilyEditViewModel_Canceled(object sender, EventArgs e)
        {
            _navigationService.CloseFamilyEditor();
        }

        void SubFamilies_EntityRemoved(object sender, System.ServiceModel.DomainServices.Client.EntityCollectionChangedEventArgs<FamilyDto> e)
        {
            DispatcherHelper.CheckBeginInvokeOnUI(() =>
             {
                 var toRem = _subFamilies.SingleOrDefault(x => x.Entity == e.Entity);
                 _subFamilies.Remove(toRem);
             });
        }

        void SubFamilies_EntityAdded(object sender, System.ServiceModel.DomainServices.Client.EntityCollectionChangedEventArgs<FamilyDto> e)
        {
            DispatcherHelper.CheckBeginInvokeOnUI(() =>
                {
                    FamilyEditViewModel vm = new FamilyEditViewModel(_dataService, _navigationService, _dataService.Families, e.Entity);
                    SubFamilies.Add(vm);
                });
        }

        protected override void OnValidateAsync(Action<bool> callback)
        {
            _dataService.FamilyExistsAsync(Entity.ProjectId, Entity.Id, Entity.Name, op =>
                {
                    if (op.HasError)
                    {
                        base.HandleDomainError(op, "Couldn't validate family name.");

                    }
                    else
                    {
                        if (op.Value)
                            Entity.ValidationErrors.Add(new System.ComponentModel.DataAnnotations.ValidationResult(Web.Resources.ValidationErrorResource.FamilyNameAlreadyExists,
                                                           new string[] { "Name" }));
                        callback(!op.Value);
                    }
                });
        }




    }
}
